﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;
using DAO;

namespace BUS
{
    public class HotelBUS
    {
        public List<HangPhongDTO> layDSHangPhong()
        {
            try
            {
                NhaCungCapDAO nccDao = new NhaCungCapDAO();
                List<NhaCungCapDTO> nccLst = nccDao.layThongTinNhaCungCapTheoLoaiNCC("Dịch vụ khách sạn");
                WebServiceDAO wsDao = new WebServiceDAO();
                WebServiceDTO ws = wsDao.layThongTinWebServiceTheoMaNCC(nccLst[0].MaNCC);
                HotelDAO htDao = new HotelDAO();
                return htDao.layDSHangPhong(ws.DiaChiWS, ws.Username, ws.Password);
            }
            catch (Exception ex)
            {
                return new List<HangPhongDTO>();
            }
        }

        public List<LoaiPhongDTO> layDSLoaiPhong()
        {
            try
            {
                NhaCungCapDAO nccDao = new NhaCungCapDAO();
                List<NhaCungCapDTO> nccLst = nccDao.layThongTinNhaCungCapTheoLoaiNCC("Dịch vụ khách sạn");
                WebServiceDAO wsDao = new WebServiceDAO();
                WebServiceDTO ws = wsDao.layThongTinWebServiceTheoMaNCC(nccLst[0].MaNCC);
                HotelDAO htDao = new HotelDAO();
                return htDao.layDSLoaiPhong(ws.DiaChiWS, ws.Username, ws.Password);
            }
            catch (Exception ex)
            {
                return new List<LoaiPhongDTO>();
            }
        }

        public List<RoomDTO> timKiemPhongMoRong(int hotelIndex, string fromdate, string todate)
        {
            try
            {
                NhaCungCapDAO nccDao = new NhaCungCapDAO();
                List<NhaCungCapDTO> nccLst = nccDao.layThongTinNhaCungCapTheoLoaiNCC("Dịch vụ khách sạn");
                WebServiceDAO wsDao = new WebServiceDAO();
                WebServiceDTO ws = wsDao.layThongTinWebServiceTheoMaNCC(nccLst[hotelIndex].MaNCC);
                HotelDAO htDao = new HotelDAO();
                List<RoomDTO> roomLst = htDao.timKiemPhongMoRong(ws.DiaChiWS, ws.Username, ws.Password, DateTime.Parse(fromdate), DateTime.Parse(todate));
                for (int i = 0; i < roomLst.Count; i++)
                {
                    roomLst[i].Gia = (int)(nccLst[hotelIndex].HeSoGia * roomLst[i].Gia);
                    roomLst[i].DonViTinh = nccLst[hotelIndex].DonViTinh;
                    roomLst[i].LoaiPhong.MoTa = roomLst[i].LoaiPhong.MoTa + "<br/><strong>Số lượng phòng còn trống: " + roomLst[i].SoChoTrong.ToString() + "</strong>";
                }
                return roomLst;
            }
            catch (Exception ex)
            {
                return new List<RoomDTO>();
            }
        }

        public List<RoomDTO> timKiemPhong(int hotelIndex, string maloaiphong, string mahangphong)
        {
            try
            {
                NhaCungCapDAO nccDao = new NhaCungCapDAO();
                List<NhaCungCapDTO> nccLst = nccDao.layThongTinNhaCungCapTheoLoaiNCC("Dịch vụ khách sạn");
                WebServiceDAO wsDao = new WebServiceDAO();
                WebServiceDTO ws = wsDao.layThongTinWebServiceTheoMaNCC(nccLst[hotelIndex].MaNCC);
                HotelDAO htDao = new HotelDAO();
                List<RoomDTO> roomLst = htDao.timKiemPhong(ws.DiaChiWS, ws.Username, ws.Password, maloaiphong, mahangphong);
                for (int i = 0; i < roomLst.Count; i++)
                {
                    roomLst[i].Gia = (int)(nccLst[hotelIndex].HeSoGia * roomLst[i].Gia);
                    roomLst[i].DonViTinh = nccLst[hotelIndex].DonViTinh;
                }
                return roomLst;
            }
            catch (Exception ex)
            {
                return new List<RoomDTO>();
            }
        }

        public List<RoomDTO> layDanhSachGiaPhong(int hotelIndex, List<LoaiPhongDTO> lpLst, List<HangPhongDTO> hpLst)
        {
            try
            {
                NhaCungCapDAO nccDao = new NhaCungCapDAO();
                List<NhaCungCapDTO> nccLst = nccDao.layThongTinNhaCungCapTheoLoaiNCC("Dịch vụ khách sạn");
                NhaCungCapDTO ncc = nccLst[hotelIndex];
                WebServiceDAO wsDao = new WebServiceDAO();
                WebServiceDTO ws = wsDao.layThongTinWebServiceTheoMaNCC(ncc.MaNCC);
                HotelDAO htDao = new HotelDAO();
                List<RoomDTO> roomLst = new List<RoomDTO>();
                for (int i = 0; i < lpLst.Count; i++)
                {
                    for (int j = 0; j < hpLst.Count; j++)
                    {
                        RoomDTO room = new RoomDTO();
                        try
                        {
                            room.LoaiPhong = lpLst[i];
                            room.HangPhong = hpLst[j];
                            room.Gia = (int)(ncc.HeSoGia * htDao.layGiaPhong(ws.DiaChiWS, ws.Username, ws.Password, lpLst[i].MaLoaiPhong, hpLst[j].MaHangPhong));
                            room.DonViTinh = ncc.DonViTinh;
                        }
                        catch
                        {
                            room.Gia = 0;
                        }
                        roomLst.Add(room);
                    }
                }
                return roomLst;
            }
            catch (Exception ex)
            {
                return new List<RoomDTO>();
            }
        }

        public LoaiPhongDTO layLoaiPhong(string mancc, string maLoaiPhong)
        {
            try
            {
                WebServiceDAO wsDao = new WebServiceDAO();
                WebServiceDTO ws = wsDao.layThongTinWebServiceTheoMaNCC(mancc);
                HotelDAO hotelDao = new HotelDAO();
                return hotelDao.layLoaiPhong(ws.DiaChiWS, ws.Username, ws.Password, maLoaiPhong);
            }
            catch (Exception ex)
            {
                return new LoaiPhongDTO();
            }
        }

        public HangPhongDTO layHangPhong(string mancc, string maHangPhong)
        {
            try
            {
                WebServiceDAO wsDao = new WebServiceDAO();
                WebServiceDTO ws = wsDao.layThongTinWebServiceTheoMaNCC(mancc);
                HotelDAO hotelDao = new HotelDAO();
                return hotelDao.layHangPhong(ws.DiaChiWS, ws.Username, ws.Password, maHangPhong);
            }
            catch (Exception ex)
            {
                return new HangPhongDTO();
            }
        }

        public RoomDTO layGiaPhong(NhaCungCapDTO ncc, string maLoaiPhong, string maHangphong)
        {
            try
            {
                WebServiceDAO wsDao = new WebServiceDAO();
                WebServiceDTO ws = wsDao.layThongTinWebServiceTheoMaNCC(ncc.MaNCC);
                HotelDAO htDao = new HotelDAO(); RoomDTO room = new RoomDTO();
                room.Gia = (int)(ncc.HeSoGia * htDao.layGiaPhong(ws.DiaChiWS, ws.Username, ws.Password, maLoaiPhong, maLoaiPhong));
                room.DonViTinh = ncc.DonViTinh;
                return room;
            }
            catch (Exception ex)
            {
                RoomDTO room = new RoomDTO();
                room.Gia = 0;
                room.DonViTinh = ncc.DonViTinh;
                return room;
            }
        }

        public string datPhong(ThongTinDatPhong ttdp)
        {
            try
            {
                WebServiceDAO wsDao = new WebServiceDAO();
                WebServiceDTO ws = wsDao.layThongTinWebServiceTheoMaNCC(ttdp.NhaCungCap.MaNCC);
                HotelDAO hotelDao = new HotelDAO();
                return hotelDao.datPhong(ws.DiaChiWS, ws.Username, ws.Password, ttdp);
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        public void thanhToan(string mahd, float soTien, string donViTinh, string transID)
        {
            try
            {
                HotelDAO hotelDao = new HotelDAO();
                hotelDao.thanhToan(mahd, soTien, donViTinh, transID);
            }
            catch (Exception ex)
            {
                return;
            }
        }
    }
}
